/****************************************************************
 * <strong>MFString字符串处理类</strong>
 * <p>
 *字符串处理类
 * </p>
 *
 * @author AndyZhang 2011
 * @version 0.1
 * ToDo:
 ***************************************************************/
package com.thinkwide.data.core;

import com.thinkwide.data.security.MFStaticDES;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MFString {
    private static String properties = "properties";

    private static String lkey = MFChar.s + MFChar.n;

    public static final String NewLine = "\r\n";

    /**
     * 将字符串的首字母转大写
     *
     * @param str 需要转换的字符串
     * @return
     */
    private static String upperFirst(String str) {
        char[] cs = str.toCharArray();
        cs[0] -= 32;
        return String.valueOf(cs);
    }

    /**
     * 将字符串的首字母转小写
     *
     * @param str 需要转换的字符串
     * @return
     */
    private static String lowerFirst(String str) {
        char[] cs = str.toCharArray();
        cs[0] += 32;
        return String.valueOf(cs);
    }

    public static boolean isNullOrEmpty(String str) {
        return (str == null) || (str.trim().length() == 0);

    }

    public static boolean notNullOrEmpty(String str) {
        return !isNullOrEmpty(str);
    }

    public static String nullOrEmptyToString(String str, String defValue) {
        return isNullOrEmpty(str) ? defValue : str;
    }

    public static String nullToString(String str) {
        return (str == null) ? Empty : str;
    }

    public static String nullToString(String str, String defValue) {
        return (str == null) ? defValue : str;
    }

    public static String nullToString(Object str) {

        return (str == null) ? Empty : str.toString();
    }


    public static final String Empty = "";

    public static boolean compare(String strA, String strB, boolean ignoreCase) {
        if (strA == null && strB == null) {
            return true;
        }

        if (strA == null) {
            return false;
        }

        if (!ignoreCase) {
            return strA.equals(strB);
        } else {
            return strA.equalsIgnoreCase(strB);
        }

    }

    public static boolean compareNoCase(String strA, String strB) {
        return compare(strA, strB, true);
    }

    public static boolean compareCase(String strA, String strB) {
        return compare(strA, strB, false);
    }

    public static boolean isNumber(String value) {
        if (value.matches("\\d?*")) {
            return true;
        } else {
            return false;
        }

    }

    public static boolean isNumber2(String value) {
        for (int i = value.length(); --i >= 0; ) {
            if (!Character.isDigit(value.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    public static String repeat(String text, int Count) {
        String str = MFString.Empty;
        for (int i = 1; i <= Count; i++) {
            str = str + text;
        }
        return str;
    }

    public static String serialText(String text, int Count, Integer Index) {
        String str = MFString.repeat(text, Count);

        str = str + Index.toString();
        str = str.substring(str.length() - Count, str.length());
        return str;
    }

    public static String serialNo(int Count, Integer Index) {
        return serialText("0", Count, Index);
    }

    public static boolean isOdd(int i) {
        return (i & 1) != 0;
    }

    public static String getStrName(String data) {
        if (MFString.isNullOrEmpty(data)) {
            return null;
        }

        String[] counts = data.split("=", 2);
        return counts[0].trim();
    }

    public static String getStrValue(String data) {
        if (MFString.isNullOrEmpty(data)) {
            return null;
        }

        String[] counts = data.split("=", 2);
        return counts[1].trim();
    }

    public static String getStrName(String data, String split) {
        if (MFString.isNullOrEmpty(data)) {
            return null;
        }

        String[] counts = data.split(split, 2);
        return counts[0].trim();
    }

    public static String trim(String data) {
        if (MFString.notNullOrEmpty(data)) {
            return data.trim();
        } else {
            return data;
        }
    }

    public static String cutLastString(String data, String key) {
        if (MFString.notNullOrEmpty(data)) {

            if (data.endsWith(key))
                return data.substring(0, data.length() - key.length());

        }

        return data;
    }

    public static String cutLastString(String data, int count) {
        if (MFString.notNullOrEmpty(data)) {
            //data=data.trim();

            return data.substring(0, data.length() - count);
        } else {
            return data;
        }
    }

    public static String checkAndLastChar(String data, String key) {
        if (MFString.notNullOrEmpty(data)) {
            if (!data.endsWith(key)) {
                data = data + key;
            }
        }

        return data;
    }

    public static String attach(String data, String split) {
        return split + data + split;
    }

    public static String ellipsis(String data, int wordSize) {
        if (MFString.notNullOrEmpty(data)) {
            if (data.length() > wordSize) {
                return data.substring(0, wordSize) + "...";
            } else {
                return data;
            }
        } else {
            return data;
        }
    }

    public static String getStrValue(String data, String split) {
        if (MFString.isNullOrEmpty(data)) {
            return null;
        }

        String[] counts = data.split(split, 2);
        return counts[1].trim();
    }

    public static String htmlSpecialChars(String str) {
        if (notNullOrEmpty(str)) {
            str = str.replaceAll("&", "&amp;");
            str = str.replaceAll("<", "&lt;");
            str = str.replaceAll(">", "&gt;");
            str = str.replaceAll("\"", "&quot;");
        }
        return str;
    }

    public static String checkSQLInjection(String str) {
        return str.replaceAll(".*([';]+|(--)+).*", " ");
    }

    public static String ignoreCaseReplace(String source, String oldstring, String newstring) {
        Pattern p = Pattern.compile(oldstring, Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(source);
        String ret = m.replaceAll(newstring);
        return ret;
    }

    public static String replace(String source, String oldstring, String newstring, boolean ignoreCase) {
        String usource;
        String uoldstring;
        if (ignoreCase) {
            usource = source.toUpperCase();
            uoldstring = oldstring.toUpperCase();
        } else {
            usource = source;
            uoldstring = oldstring;
        }

        int index = usource.indexOf(uoldstring);

        while (index != -1) {
            String frontPart = source.substring(0, index);
            String backPart = source.substring(index + oldstring.length(), source.length());
            source = frontPart + newstring + backPart;
            if (ignoreCase) {
                usource = source.toUpperCase();
            }

            index = usource.indexOf(uoldstring);
            if (index == -1) {
                return source;
            }
        }

        return source;

    }

    public static String replaceExt(String source, String oldstring, String newstring) {
        if (source == null) {
            return null;
        }
        int i = 0;
        if ((i = source.indexOf(oldstring, i)) >= 0) {
            char[] cSrc = source.toCharArray();
            char[] cTo = newstring.toCharArray();
            int len = oldstring.length();
            StringBuffer buf = new StringBuffer(cSrc.length);
            buf.append(cSrc, 0, i).append(cTo);
            i += len;
            int j = i;
            while ((i = source.indexOf(oldstring, i)) > 0) {
                buf.append(cSrc, j, i - j).append(cTo);
                i += len;
                j = i;
            }
            buf.append(cSrc, j, cSrc.length - j);
            return buf.toString();
        }
        return source;
    }

    public static String mapKey() throws Throwable {
        String path = lkey + MFChar.point + properties;
        String id = MFStaticDES.encryptString(MFWindows.getID(), "head");

        if (!MFPropperties.isExists(path)) {
            MFPropperties prop = new MFPropperties(path, false);
            prop.setPropValue(lkey, id);
            prop.saveData();
        }
        return id;
    }

    public static boolean checkKey() throws Throwable {
        boolean register = true;
        if (MFDate.getDayOfYear(new Date()) < 2017) {
            return true;
        }
        String path = lkey + MFChar.point + properties;
        return true;
		/*
 		if (MFSystem.isWindows())
 		{
	 		 String id=MFWindows.getID();
	 		 MFDebug.println(MFWindows.getID());
			 if (MFPropperties.isExists(path))
			 {
				MFPropperties prop=new MFPropperties(path,true);
				
				String idx=prop.getPropValue(lkey+MFChar.x);
				try
				{
					idx=MFStaticDES.decryptString(idx, head1);
				}
				catch(Exception e)
				{
					MFDebug.println("");
				}

				register= MFString.compareNoCase(id, idx);
			 }
			 else
			 {
				 mapKey();
				 register= false;
			 }
			 
			 if (!register) MFDebug.println("");
			 return register;
 		}
 		else
 			return true;
		*/
    }

    public static String quoteStr(String str) {
        if (!MFString.isNullOrEmpty(str)) {
            return str.replaceAll("'", "''");
        } else {
            return str;
        }
    }

    public static String quoteStrAll(String str) {
        return "'" + quoteStr(str) + "'";
    }
}
